package cn.edu.wfit.modules.infomanagement.service.impl;

import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.infomanagement.domain.TBus;
import cn.edu.wfit.modules.infomanagement.domain.TBusDate;
import cn.edu.wfit.modules.infomanagement.domain.TBusReservation;
import cn.edu.wfit.modules.infomanagement.domain.TBusSchedule;
import cn.edu.wfit.modules.infomanagement.dto.TeacherReservationDetial;
import cn.edu.wfit.modules.infomanagement.mapper.TBusDateMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusReservationMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusScheduleMapper;
import cn.edu.wfit.modules.infomanagement.service.ITBusReservationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 存储教师预约记录Service业务层处理
 *
 * @author zhanghao
 * @date 2025-11-07
 */
@Service
public class TBusReservationServiceImpl implements ITBusReservationService {
    @Autowired
    private TBusReservationMapper tBusReservationMapper;
    @Autowired
    private TBusDateMapper tBusDateMapper;
    @Autowired
    private TBusScheduleMapper tBusScheduleMapper;
    @Autowired
    private TBusMapper tBusMapper;

    /**
     * 查询存储教师预约记录
     *
     * @param id 存储教师预约记录主键
     * @return 存储教师预约记录
     */
    @Override
    public TBusReservation selectTBusReservationById(Long id) {
        return tBusReservationMapper.selectTBusReservationById(id);
    }

    /**
     * 查询存储教师预约记录列表
     *
     * @param tBusReservation 存储教师预约记录
     * @return 存储教师预约记录
     */
    @Override
    public List<TBusReservation> selectTBusReservationList(TBusReservation tBusReservation) {
        return tBusReservationMapper.selectTBusReservationList(tBusReservation);
    }

    /**
     * 新增存储教师预约记录
     *
     * @param tBusReservation 存储教师预约记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTBusReservation(TBusReservation tBusReservation) {
        //虽然名字是updateTime,但实际上是创建时间,因为没招了,懒得改了,就直接用updateTime了
        tBusReservation.setUpdateTime(DateUtils.getNowDate());
        Long dateId = tBusReservation.getDateId();
        TBusDate tBusDate = tBusDateMapper.selectTBusDateByIdForUpdate(dateId);
        Date departureData = tBusDate.getDepartureData();
        tBusReservation.setCreateTime(departureData);
        /*获取荷载人数,若当前预约成功人数大于荷载人数,则报名状态为候补中*/
        Long maxCapacit = tBusDate.getMaxCapacit();
        TBusReservation queryReservation = new TBusReservation();
        queryReservation.setDateId(dateId);
        queryReservation.setReservationStatus(1L);
        List<TBusReservation> tBusReservations = tBusReservationMapper.selectTBusReservationList(queryReservation);
        if (tBusReservations.size() >= maxCapacit) {
            tBusReservation.setReservationStatus(2L);
        } else {
            tBusReservation.setReservationStatus(1L);
        }

        return tBusReservationMapper.insertTBusReservation(tBusReservation);
    }

    /**
     * 修改存储教师预约记录
     *
     * @param tBusReservation 存储教师预约记录
     * @return 结果
     */
    @Override
    public int updateTBusReservation(TBusReservation tBusReservation) {
        //如果用户取消了,则根据候补顺序,将下一个候补用户预约状态改为已预约
        //首先查询当前用的状态
        Long reservationId = tBusReservation.getId();
        TBusReservation currentReservation = tBusReservationMapper.selectTBusReservationById(reservationId);
         if (currentReservation.getReservationStatus() == 2L) {
            System.out.println("当前用户为候补用户,无需为下一位候补更新状态");
        }else {
            if (tBusReservation.getReservationStatus() == 3L) {
                //根据预约日期id查询预约记录
                List<TBusReservation> tBusReservations = tBusReservationMapper.selectTBusReservationByDateId(tBusReservation.getDateId());
                //根据预约记录的预约顺序,将下一个候补用户预约状态改为已预约
                for (int i = 0; i < tBusReservations.size(); i++) {
                    TBusReservation nextReservation = tBusReservations.get(i);
                    //如果是候补中用户,则将候补中用户预约状态改为已预约
                    if (nextReservation.getReservationStatus() == 2L) {
                        //如果是当前用户,则跳过
                        if (nextReservation.getId().equals(reservationId)) {
                            continue;
                        }
                        nextReservation.setReservationStatus(1L);
                        tBusReservationMapper.updateTBusReservation(nextReservation);
                        break;
                    }
                }
            }
        }
        return tBusReservationMapper.updateTBusReservation(tBusReservation);
    }

    /**
     * 批量删除存储教师预约记录
     *
     * @param ids 需要删除的存储教师预约记录主键
     * @return 结果
     */
    @Override
    public int deleteTBusReservationByIds(Long[] ids) {
        return tBusReservationMapper.deleteTBusReservationByIds(ids);
    }

    /**
     * 删除存储教师预约记录信息
     *
     * @param id 存储教师预约记录主键
     * @return 结果
     */
    @Override
    public int deleteTBusReservationById(Long id) {
        return tBusReservationMapper.deleteTBusReservationById(id);
    }

    /**
     * 通过教师工号和预约状态查询存储教师预约记录列表
     *
     * @param tBusReservation 存储教师预约记录
     * @return
     */
    @Override
    public List<TeacherReservationDetial> selectTBusReservationListByTeacherIdAndStatus(TBusReservation tBusReservation) {
        List<TeacherReservationDetial> teacherReservationDetials = new ArrayList<>();
        List<TBusReservation> tBusReservations = tBusReservationMapper.selectTBusReservationListByTeacherIdAndStatus(tBusReservation);
        for (int i = 0; i < tBusReservations.size(); i++) {
            TBusReservation list = tBusReservations.get(i);

            //创建新的类
            TeacherReservationDetial teacherReservationDetial = new TeacherReservationDetial();
            BeanUtils.copyProperties(list, teacherReservationDetial);

            //获取预约日期id
            Long dateId = list.getDateId();

            //根据预约日期id查询预约日期信息
            TBusDate tBusDate = tBusDateMapper.selectTBusDateById(dateId);
            if (tBusDate.getStatus() != 2L) {
                //将预约日期信息设置到预约记录中
                teacherReservationDetial.settBusDate(tBusDate);
                //获取排班id
                Long scheduleId = tBusDate.getScheduleId();

                //根据排班id查询排班信息
                TBusSchedule tBusSchedule = tBusScheduleMapper.selectTBusScheduleById(scheduleId);
                if (tBusSchedule != null && tBusSchedule.getStatus() != 2L) {
                    //将排班信息设置到预约记录中
                    teacherReservationDetial.settBusSchedule(tBusSchedule);
                    //获取公交车id
                    Long busId = tBusSchedule.getBusId();

                    //根据公交车id查询公交车信息
                    TBus tBus = tBusMapper.selectTBusById(busId);

                    //将公交车信息设置到预约记录中
                    teacherReservationDetial.settBus(tBus);
                    //将预约记录添加到列表中
                    teacherReservationDetials.add(teacherReservationDetial);
                }
            }
        }
        return teacherReservationDetials;
    }

    @Override
    public TBusReservation selectTBusReservationByTeacherIdAndDateId(TBusReservation tBusReservation) {
        return tBusReservationMapper.selectTBusReservationByTeacherIdAndDateId(tBusReservation);
    }
}
